package aidl.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AidlTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        System.out.println("aidl.getId():"+Thread.currentThread().getId());
        
        Intent intent = new Intent(AidlTest.this, MyService.class);
        
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        
        startService(intent);
	    
        button = (Button) findViewById(R.id.mybutton);
        button.setOnClickListener(myListener);
        
        
        findViewById(R.id.mybutton1).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(AidlTest.this,AidlTest2.class));
			}
		});
    }
    
    private OnClickListener myListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
    	    try {
    			iServiceBinder.showSomethings(1);
    		} catch (RemoteException e) {
    			e.printStackTrace();
    		}
		}
	};
    
    private Button button = null;
    
    private IServiceBinder iServiceBinder = null;
    
    private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			iServiceBinder = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			iServiceBinder = IServiceBinder.Stub.asInterface(service);
		}
	};
    
    
}